//
//  SDRetroactionController.m
//  SuperDoctor
//
//  Created by 龚睿 on 16/7/6.
//  Copyright © 2016年 SD. All rights reserved.
//

#import "SDRetroactionController.h"
#import <SVProgressHUD.h>

@interface SDRetroactionController ()<UITextViewDelegate>

//记录输入的字符串lable
@property(nonatomic,strong)UILabel *lb_tickling;

//设置textView占位字符串的UIlable
@property(nonatomic,strong)UILabel *lb_Placeholder;

@property(nonatomic,strong)UITextView *tv_retroaction;

@end

@implementation SDRetroactionController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupUI];
    
}


#pragma mark
#pragma mark - 设置界面
- (void)setupUI{
    
    
    //    设置textView
    UITextView *tv_retroaction = [[UITextView alloc] init];
    
    tv_retroaction.textColor = [UIColor whiteColor];
    
    [self.view addSubview:tv_retroaction];
    
   [tv_retroaction mas_makeConstraints:^(MASConstraintMaker *make) {
       
       // 顶部的约束
       make.top.mas_equalTo(self.mas_topLayoutGuideBottom).offset(10);
       
       // 右侧的约束
       make.right.mas_equalTo(self.view.mas_right).offset(-5);
       
       // 左侧的约束
       make.left.mas_equalTo(self.view.mas_left).offset(5);
       
       // 高度的约束
       make.height.mas_equalTo(150);
       
   }];
    tv_retroaction.backgroundColor = [UIColor lightGrayColor];

    
    tv_retroaction.font = [UIFont systemFontOfSize:17];
    self.tv_retroaction = tv_retroaction;
    

//        设置属性lable
    UILabel *lb_Placeholder = [[UILabel alloc] init];
    
    lb_Placeholder.frame = CGRectMake(5, 0, 200, 20);
    lb_Placeholder.font = [UIFont systemFontOfSize:17];
    
    self.lb_Placeholder = lb_Placeholder;
    self.lb_Placeholder.text = @"请输入反馈意见";
    self.lb_Placeholder.textColor = [UIColor blackColor];
    self.lb_Placeholder.enabled = NO;
    self.lb_Placeholder.backgroundColor = [UIColor clearColor];
    
    
    
    [self.tv_retroaction addSubview:self.lb_Placeholder];
    
//    设置代理
    tv_retroaction.delegate = self;
    tv_retroaction.backgroundColor = [UIColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0];

    UIButton *btn_retroaction = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 80, CGRectGetMaxY(tv_retroaction.frame) + 5, 75, 30)];
    
    
    
    
    //   设置lable
    UILabel *lb_tickling = [[UILabel alloc] init];
    [self.view addSubview:lb_tickling];

//   设置约束
    [lb_tickling mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tv_retroaction.mas_bottom).offset(5);
        make.left.mas_equalTo(self.view.mas_left).offset(30);
        
        make.height.mas_equalTo(30);
        
        make.width.mas_equalTo(210);
        
        
    }];
    
    lb_tickling.textAlignment = NSTextAlignmentLeft;
    self.lb_tickling = lb_tickling;
    
     self.lb_tickling.text = [NSString stringWithFormat:@"   您还可以输入的字数:200"];
    
    //    设置button
    [btn_retroaction setBackgroundColor:[UIColor colorWithRed:58.0/255 green:198.0/255 blue:197.0/255 alpha:1.0]];
    [btn_retroaction setTitle:@"提交反馈" forState:UIControlStateNormal];
    [self.view addSubview:btn_retroaction];
    [btn_retroaction mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tv_retroaction.mas_bottom).offset(2);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(75);
             
    }];
    
    UILabel *lb_Contact = [[UILabel alloc]init];
    [self.view addSubview:lb_Contact];
    
    lb_Contact.text = @"  欢迎联系我们";
    lb_Contact.textAlignment = NSTextAlignmentLeft;
    [lb_Contact setBackgroundColor:[UIColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0]];
    
[lb_Contact mas_makeConstraints:^(MASConstraintMaker *make) {
    
    make.top.mas_equalTo(btn_retroaction.mas_bottom).offset(5);
    make.height.mas_equalTo(30);
    make.left.mas_equalTo(self.view.mas_left).offset(0);
    
    make.width.mas_equalTo(self.view.frame.size.width);
    
}];
    
    //    设置imageview
    UIImageView *iv_iphone = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dianhua"]];
    [self.view addSubview:iv_iphone];

    
   [iv_iphone mas_makeConstraints:^(MASConstraintMaker *make) {
       
       make.top.mas_equalTo(lb_Contact.mas_bottom).offset(5);
       make.left.mas_equalTo(self.view.mas_left).offset(5);
       make.width.mas_equalTo(16);
       make.height.mas_equalTo(18);
       
       
   }];
    
        UILabel *lb_iphone = [[UILabel alloc] init];
    [self.view addSubview:lb_iphone];
    lb_iphone.textAlignment = NSTextAlignmentLeft;
    lb_iphone.text = @"官方电话: 400-636-6113";
    
    [lb_iphone mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(iv_iphone.mas_right).offset(5);
        make.width.mas_equalTo(200);
        make.centerY.mas_equalTo(iv_iphone.mas_centerY);
        
        
    }];

    
    
    UIImageView *iv_exchange = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"qq"]];
    [self.view addSubview:iv_exchange];
    [iv_exchange mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(iv_iphone.mas_bottom).offset(5);
        make.left.mas_equalTo(self.view.mas_left).offset(5);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(18);
        
    }];


    
    
    UILabel *lb_exchange = [[UILabel alloc] init];
    [self.view addSubview:lb_exchange];
    
    lb_iphone.textAlignment = NSTextAlignmentLeft;
    lb_exchange.text = @"用户交流群: QQ 464768569";
    
    [lb_exchange mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iv_exchange.mas_right).offset(5);
        make.width.mas_equalTo(251);
        make.centerY.mas_equalTo(iv_exchange.mas_centerY);
        

    }];
    
//    添加监听点击方法
    [btn_retroaction addTarget:self action:@selector(didClickCommitBtn) forControlEvents:UIControlEventTouchUpInside];
  
    
}


#pragma mark
#pragma mark -提交btn的监听点击方法
- (void)didClickCommitBtn{

    [self.navigationController popViewControllerAnimated:NO];


}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if (range.location > 200) {
        return  NO;
    }else if ([text isEqualToString:@""]){
    
        return YES;
    }
        
        return YES;
}


- (void)textViewDidBeginEditing:(UITextView *)textView{
    
    [self.lb_Placeholder removeFromSuperview];


}


#pragma mark
#pragma mark - textView代理方法
- (void)textViewDidChange:(UITextView *)textView{
    
    self.lb_tickling.text = [NSString stringWithFormat:@"   您还可以输入的字数:%zd",200 - textView.text.length];
    
    
//    设置最大输入限制
    if (textView.text.length >200) {
        
        self.lb_tickling.text = [NSString stringWithFormat:@"   您还可以输入的字数:0"];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        [SVProgressHUD showErrorWithStatus:@"超出了哦"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });

        
       
    }

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.tv_retroaction endEditing:YES];
    

}

@end
